/**
 * 防抖指令
 */
const debounce = {
  /**
   * @description 指令初始化
   * @param {*} el
   * @param {*} binding
   */
  mounted(el, binding) {
    if (typeof binding.value !== "function") {
      throw "callback 需是 function";
    }
    let timer = null;
    el.__handleClick__ = function () {
      if (timer) {
        clearInterval(timer);
      }
      timer = setTimeout(() => {
        binding.value();
      }, 500);
    };
    el.addEventListener("click", el.__handleClick__);
  },

  /**
   * @description 移除事件
   * @param {*} el
   */
  beforeUnmount(el) {
    el.removeEventListener("click", el.__handleClick__);
  },
};

export default debounce;
